package org.example.mall.admin.controller.manage;


import cn.hutool.core.lang.tree.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.example.mall.admin.constant.vaild.C;
import org.example.mall.admin.constant.vaild.Q;
import org.example.mall.admin.constant.vaild.U;
import org.example.mall.admin.model.entity.manage.SysTree;
import org.example.mall.admin.model.vo.manage.SysTreeVo;
import org.example.mall.admin.service.manage.SysTreeService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.PermissionUtil;
import org.example.mall.admin.util.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统分类树 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2022-01-08
 */
@Api(tags = "系统分类树")
@RestController
@RequestMapping("/manage/sysTree")
@AllArgsConstructor
public class SysTreeController {

    private final SysTreeService treeService;

    @GetMapping("/{treeId}")
    @ApiOperation("通过id查询分类树信息")
    public R<SysTreeVo> getByTreeId(@PathVariable Integer treeId) {
        return R.ofSuccess(treeService.getByTreeId(treeId));
    }

    @ApiOperation("返回分类树形菜单集合")
    @GetMapping(value = "/tree")
    public R<List<Tree<Integer>>> getTree(@Validated(Q.class) SysTree tree) {
        return R.ofSuccess(treeService.selectTree(tree));
    }

    @PostMapping
    @ApiOperation("创建分类树")
    public R<Boolean> add(@RequestBody @Validated(C.class) SysTree sysTree) {
        PermissionUtil.checkSysTree(sysTree.getTreeType(), "add");
        return R.ofSuccess("创建分类树成功",
                BusOp.mustSucceed(treeService.add(sysTree), "创建分类树失败"));

    }

    @PutMapping
    @ApiOperation("修改分类树")
    public R<Boolean> edit(@RequestBody @Validated(U.class) SysTree sysTree) {
        PermissionUtil.checkSysTree(sysTree.getTreeType(), "edit");
        return R.ofSuccess("修改分类树成功",
                BusOp.mustSucceed(treeService.edit(sysTree), "修改分类树失败"));
    }

    @ApiOperation("删除分类树")
    @DeleteMapping("/{treeId}")
    public R<Boolean> removeById(@PathVariable Integer treeId) {
        SysTree sysTree = treeService.getById(treeId);
        BusOp.mustNotNull(sysTree, "分类树不存在");
        PermissionUtil.checkSysTree(sysTree.getTreeType(), "del");
        return R.ofSuccess("删除分类树成功",
                BusOp.mustSucceed(treeService.del(sysTree), "删除分类树失败"));
    }

}

